#include <Precomp.h>
#include "defs.h"
#include "Application.h"
#include "ServerConnection.h"
#include "ClientListener.h"
#include "BaseGameState.h"
#include "GameStateCreateNewLogin.h"
#include "packet.h"
#include "GameWorld.h"
#include "UIManager.h"

/* Ok. We need to play games with states here -- If we specify a username and password, it'll just create an account. We need to verify one exists. 
   State tables, here we come. */

void CGameStateCreateNewLogin::Initialize(CNetrekClientListener* pListener)
{
    CBaseGameState::Initialize(pListener);

    m_eLoginState = eLoginState_Idle;
}

void CGameStateCreateNewLogin::FrameStart()
{
    m_pListener->GetServerConnection()->UpdateWithPacketSink(this);
}

void CGameStateCreateNewLogin::Cleanup()
{
}

bool CGameStateCreateNewLogin::ProcessPacket(EServerPacketType ePacketType, const packet_base* pPacket)
{
    if(ePacketType == EServerPacketType_Login)
    {
        const login_spacket* pLoginPacket = (const login_spacket*) pPacket;

        if(pLoginPacket->accept)
        {
            if(m_eLoginState == eLoginState_Verifying)
            {
                m_pListener->GetUIManager()->ShowOKDialog(L"Sorry. An account with that name already exists.", DialogResultCallback(&CGameStateCreateNewLogin::ErrorDialogOK, this));
                m_eLoginState = eLoginState_Idle;
            }
            else if(m_eLoginState == eLoginState_LoggingIn)
            {
                m_pListener->MoveToNewState(eNetrekClientState_TeamAndShipSelect);
            }
        }
        else
        {
            if(m_eLoginState == eLoginState_Verifying)
            {
                // No user account exists with that name. 
                m_pListener->GetServerConnection()->SendLoginRequest(m_szLoginName, m_szLoginPass, "", false);
                m_eLoginState = eLoginState_LoggingIn;
            }
            else
            {
                m_pListener->GetUIManager()->ShowOKDialog(L"Error when logging in.", DialogResultCallback(&CGameStateCreateNewLogin::ErrorDialogOK, this));
                m_eLoginState = eLoginState_Idle;
            }
        }

        return false;
    }

    m_pListener->GetGameWorld()->ProcessPacket(ePacketType, pPacket);

    return true;
}

void CGameStateCreateNewLogin::CancelButtonClicked()
{
    if(m_eLoginState != eLoginState_Idle)
    {
        return;
    }

    m_pListener->MoveToNewState(eNetrekClientState_Login);
    return;
}


/*
static bool IsWhitespace(const char* szText)
{
    int iLen = strlen(szText);

    for(int i = 0; i < iLen; i++)
    {
        if(!isspace(szText[i]))
        {
            return false;
        }
    }
    return true;
}
*/

void CGameStateCreateNewLogin::CreateButtonClicked()
{
    /*
    if(m_eLoginState != eLoginState_Idle)
    {
        return true;
    }

    const char* szLoginName = GET_CEGUI_TEXT("Login");
    const char* szLoginPass = GET_CEGUI_TEXT("Password");
    const char* szLoginPassVerify = GET_CEGUI_TEXT("PasswordVerify");

    if(IsWhitespace(szLoginName))
    {
        m_pListener->GetUIManager()->ShowOKDialog(L"Invalid login name.", DialogResultCallback(ErrorDialogOK, this));
        return true;
    }

    if(strcmp(szLoginPass, szLoginPassVerify))
    {
        m_pListener->GetUIManager()->ShowOKDialog(L"Passwords do not match.", DialogResultCallback(ErrorDialogOK, this));
        return true;
    }

    strncpy(m_szLoginName, szLoginName, sizeof(m_szLoginName));
    strncpy(m_szLoginPass, szLoginPass, sizeof(m_szLoginPass));

    m_eLoginState = eLoginState_Verifying;
    m_pListener->GetServerConnection()->SendLoginRequest(szLoginName, szLoginPass, "", true);

*/
}

